import 'package:day_demo/src/flutter_page_flash/flutter_page_state.dart';
import 'package:flutter/material.dart';

class FlutterPageFlash extends StatefulWidget {
  const FlutterPageFlash({
    super.key,
    required this.child,
    required this.controller,
    this.loadingWidget,
    this.switcherAnimation = true,
  });
  final Widget child;
  final Widget? loadingWidget;
  final FlutterPageController controller;
  final bool switcherAnimation;
  @override
  State<FlutterPageFlash> createState() => _FlutterPageFlashState();
}

class _FlutterPageFlashState extends State<FlutterPageFlash> {
  late FlutterPageController controller = widget.controller;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: controller,
      builder: (ctx, state, child) {
        if (widget.switcherAnimation) {
          Widget loadingWidget = widget.loadingWidget ??
              Container(
                color: Colors.white,
              );
          return AnimatedSwitcher(
            duration: const Duration(milliseconds: 1000),
            child: state == FlutterPageState.loading ? loadingWidget : child,
          );
        }
        if (state == FlutterPageState.loading) {
          return widget.loadingWidget ??
              const Center(
                  child: CircularProgressIndicator(
                color: Color(0xFF4D5EFF),
                valueColor: AlwaysStoppedAnimation(Color(0xFF4D5EFF)),
              ));
        }
        return child!;
      },
      child: widget.child,
    );
  }
}
